/**
* @Title: auditFeedback_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年11月19日 上午15:06:02
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	//定义变量
	app.value("MonitorIntegrity",{
		
	});
	app.controller('MonitorIntegrityCtrl', ['$scope','$state','MonitorIntegrity','FwaTabsetService','CommunityService','Modal','MonitorIntegrityService',
	           function($scope,$state,MonitorIntegrity,FwaTabsetService,CommunityService,Modal,MonitorIntegrityService){
		
		$scope.monitorIntegrity = MonitorIntegrity;
		
		CommunityService.initCodeListDatas();
		$scope.communityService = CommunityService;
		var isFlag = true;
		$scope.$watch('communityService.orgLevelDatas',function(v){
			
			if (v) {
				angular.forEach(v,function(data,index){
					data.checked = true;
				});
				v.unshift({name:'全部',checked:true});
				
				if (isFlag
					&& $scope.date ) {
					$scope.query();
					isFlag = false;
				}
			}
			
		});
		
		$scope.$watch('communityService.caseTypeDatas',function(v){
			
			if (v) {
				angular.forEach(v,function(data,index){
					data.checked = true;
				});
				v.unshift({name:'全部',checked:true});
			}
			
		});
		//折线图
		$scope.lineOption = {
				grid:{
					y:5,
					y2:30,
					x:35,
					x2:25
				},
				tooltip : {
					show:false
				},
				xAxis : [{
		             type : 'category',
		             boundaryGap: false,
		             splitLine:{
			            	show:false
			         },
			         axisLine:{
			        	lineStyle:{
				        	color: '#7b8cc8', 
				        	width: 1, 
				        	type: 'solid'	
			        	}
			         },
		             data : ['']
		         }],
		         yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value}'
					            },
					            axisLine:{
				            		lineStyle:{
				            			width:0
				            		}
				            	}
					        }
				  ],
				  color:['#107fcd'],
		         series:[]
			};
		
		//折线图
		$scope.newLineOption = {
				 title : {
				        text: '诚信违规案件趋势图',
				        x:'center'
			    },
			    legend: {
			        orient : 'vertical',
			        x : 'right',
			        data:['案件数']
			    },
				grid:{
					y:40,
					y2:30,
					x:35,
					x2:25
				},
				tooltip : {
					show:false
				},
				xAxis : [{
		             type : 'category',
		             boundaryGap: false,
		             splitLine:{
			            	show:false
			         },
			         axisLine:{
			        	lineStyle:{
				        	color: '#7b8cc8', 
				        	width: 1, 
				        	type: 'solid'	
			        	}
			         },
		             data : ['']
		         }],
		         yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value}'
					            },
					            axisLine:{
				            		lineStyle:{
				            			width:0
				            		}
				            	}
					        }
				  ],
				  color:['#107fcd'],
		         series:[]
			};
		//饼图
		$scope.pieOption = {
						grid:{
							y:5,
							y2:5,
							x:5,
							x2:5
						},
						tooltip : {
					        trigger: 'item'
					        ,formatter: "{b}: {c}例 ({d}%)"
					    },
						color:['#0066b0','#137ece','#2d97e5','#6ab1e7'],
						series:[{
							type:'pie',
							radius:'80%',
							center:['50%','50%'],
							itemStyle : {
				                normal : {
				                    label : {
				    					formatter:'{b}\n({d}%)'
				                    }
				                }
				            },
							data:[]
						}]
					};
		
		//查询
		$scope.query  = function () {
			
			var startDate = '',endDate = '';
			
			if ($scope.date 
				&&$scope.date.start) {
				startDate = $scope.date.start;
			}
			
			if ($scope.date 
					&&$scope.date.end) {
				endDate = $scope.date.end;
			}
			var temp = CommunityService.checkDateSpan(startDate,endDate);
			if (temp != '') {
				Modal.alert(temp);
				return false;
			}
			
			var orgLevels = [],caseTypes = [];
			angular.forEach($scope.communityService.orgLevelDatas,function(data,index){
				if (data.code 
					&& data.checked == true) {
					orgLevels.push(data.code);
				}
			});
			
			angular.forEach($scope.communityService.caseTypeDatas,function(data,index){
				if (data.code 
					&& data.checked == true) {
					caseTypes.push(data.code);
				}
			});
			var params = {
					orgLevels: orgLevels.join(","),
					caseTypes : caseTypes.join(","),
					startDate : startDate,
					endDate : endDate
			};
			MonitorIntegrity.params = params;
			monitorIntegrityMap();
			
			MonitorIntegrityService.query(params).success(function(res){
				angular.forEach(res.levelData,function(data,index){
					angular.forEach($scope.communityService.orgLevelDatas,function(orgData,index){
						if (orgData.code == data.name) {
							data.name = orgData.name;
						}
					});
				});
				
				$scope.countNum = res.countNum;
				var xAxis = getXAxis(res.data);
				var series = getSeries(res.caseData);
				
				$scope.lineOption.xAxis = xAxis;
				$scope.lineOption.series = series;
				
				$scope.newLineOption.xAxis = xAxis;
				$scope.newLineOption.series = series;
				
				$scope.pieOption.series = getPieSeries(res.levelData);
				
			});
		}
		
		function getPieSeries(data){
			return [{
				type:'pie',
				radius:'60%',
				center:['50%','58%'],
				itemStyle : {
	                normal : {
	                    label : {
	                    	formatter:'{b}({d}%)'
	                    }
	                }
	            },
				data:data
			}];
		}
		//日期
	   function getXAxis(data) {
		   return {
	             type : 'category',
	             boundaryGap: false,
	             splitLine:{
		            	show:false
		         },
		         axisLine:{
		        	lineStyle:{
			        	color: '#7b8cc8', 
			        	width: 1, 
			        	type: 'solid'	
		        	}
		         },
	             data : data
	         };
	   }
	   
	   //series 
	   function getSeries(caseData) {
		   return [{
				type:'line',
				name:'案件数',
				data: caseData
				}];
	   }
		//默认日期 当前日期的前一个月
		CommunityService.initDate("yyyy-MM").success(function(res) {
			var endDate = res.formatDate;
			var startDate = CommunityService.lastYear(endDate);
			$scope.date = {
					start: startDate,
					end:endDate
			};
			if (isFlag
				&& $scope.communityService.orgLevelDatas ) {
				$scope.query();
				isFlag = false;
			}
		});
		
		$scope.integrityShow = false;
		//隐藏
		$scope.mouseoutShow = function() {
			$scope.integrityShow = false;
		}
		
		//显示
		$scope.mouseoverShow = function() {
			$scope.integrityShow = true;
		}
		
		//机构类型
		$scope.checkOrgLevel = function(checkData){
			if (checkData.code) {
				checkData.checked = checkData.checked;
				var length = 0;
				angular.forEach($scope.communityService.orgLevelDatas,function(data,index){
					if (data.code
						&& data.checked  === true) {
						length++;
					}
				});
				if (($scope.communityService.orgLevelDatas.length - 1) == length ) {
					$scope.communityService.orgLevelDatas[0].checked = true;
				}
				else {
					$scope.communityService.orgLevelDatas[0].checked = false;
				}
			}
			else {
				angular.forEach($scope.communityService.orgLevelDatas,function(data,index){
					data.checked = checkData.checked;
				});
			}
		}
		
		//案件类型
		$scope.checkCaseType = function(checkData){
			if (checkData.code) {
				checkData.checked = checkData.checked;
				var length = 0;
				angular.forEach($scope.communityService.caseTypeDatas,function(data,index){
					if (data.code
						&& data.checked  === true) {
						length++;
					}
				});
				if (($scope.communityService.caseTypeDatas.length - 1) == length ) {
					$scope.communityService.caseTypeDatas[0].checked = true;
				}
				else {
					$scope.communityService.caseTypeDatas[0].checked = false;
				}
			}
			else {
				angular.forEach($scope.communityService.caseTypeDatas,function(data,index){
					data.checked = checkData.checked;
				});
			}
		}
		
		
		//医疗机构
//		$scope.orgDatas = [
//		                   {orgName:"八五医院",score:"60",width:"60"},
//		                   {orgName:"第一人民医院",score:"70",width:"70"},
//		                   {orgName:"第二人民医院",score:"65",width:"65"},
//		                   {orgName:"仁济医院",score:"53",width:"53"}
//		                   ];
		//定点药店
//		$scope.pharmacyDatas = [
//		                   {pharmacyName:"百姓大药房",score:"67",width:"67"},
//		                   {pharmacyName:"国药药房",score:"68.5",width:"68.5"},
//		                   {pharmacyName:"邦康药房",score:"69.5",width:"69.5"}
//		                   ];
		
		MonitorIntegrityService.queryKeyMonitoring().success(function(res){
			$scope.staffDatas = res.staffDatas;
			//医务人员
			angular.forEach($scope.staffDatas,function(data,index){
				data.score = data.total - data.score;
				data.width = data.score > 0 ?((data.score/data.total)*100).toFixed(2) : (0 - data.score);
			});
			//参保人
			$scope.insuredDatas = res.insuredDatas;
		});
		//加载地图
		function monitorIntegrityMap() {
			$state.go('monitorIntegrity.monitorIntegrityMap');
		}
		

		//医疗机构跳转
		$scope.skipOrg = function() {
			Modal.alert("功能后续进行完善");
			//FwaTabsetService.selectTab("");
		}
		
		//定点药店跳转
		$scope.skipPharmacy = function() {
			Modal.alert("功能后续进行完善");
			//FwaTabsetService.selectTab("");
		}
		
		//医务人员跳转
		$scope.skipStaff = function() {
			var startDate = $scope.date.start + '-01';
			var endDate = $scope.date.end + '-01';
			FwaTabsetService.selectTab("integrityMedical.integrityList",{
				startDate : startDate,
    			endDate : endDate
			});
		}
		
		//参保人跳转
		$scope.skipInsured = function() {
			var startDate = $scope.date.start + '-01';
			var endDate = $scope.date.end + '-01';
			FwaTabsetService.selectTab("insuredBlacklist.insuredList",{
				startDate : startDate,
    			endDate : endDate
			});
		}
		
	}]);
})();
